package com.bi.onlineEducation.web.controller;

import com.bi.onlineEducation.annotation.WebProcess;
import com.bi.onlineEducation.domain.CourseTypePo;
import com.bi.onlineEducation.exception.BusinessHandleException;
import com.bi.onlineEducation.service.CourseTypeService;
import com.bi.onlineEducation.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author: chenYuTao
 * @Time: 2022/1/6 15:44
 * @Description: 基础数据控制层
 **/
@RestController
@Api(tags = "课程类型信息控制层", description = "课程类型信息数据接口")
public class CourseTypeController {

    @Resource
    private CourseTypeService courseTypeService;

    @ApiOperation("查询所有")
    @WebProcess
    @GetMapping(value = "/courseType/findAll")
    public ResultCode findAll() {
        return ResultCode.success(courseTypeService.findAll());
    }

    @ApiOperation("根据id查询课程类型信息")
    @WebProcess
    @GetMapping(value = "/courseType/findById")
    public ResultCode findById(@RequestParam("id") Integer id) {
        return ResultCode.success(courseTypeService.findById(id));
    }

    @ApiOperation("课程类型搜索所有")
    @WebProcess
    @GetMapping(value = "/courseType/search")
    public ResultCode search(@RequestParam("ctName") String ctName, @RequestParam("realName") String realName) {
        return ResultCode.success(courseTypeService.search(ctName, realName));
    }

    @ApiOperation("保存信息")
    @WebProcess
    @PostMapping(value = "/courseType/save")
    public ResultCode save(@RequestBody CourseTypePo courseTypePo) {
        Optional.ofNullable(courseTypePo.getLId()).orElseThrow(() -> BusinessHandleException.buildBizException("创建人不能为空"));
        courseTypePo.setCtCreateTime(ObjectUtils.isEmpty(courseTypePo.getCtCreateTime()) ? new Date() : courseTypePo.getCtCreateTime());
        return ResultCode.success(courseTypeService.save(courseTypePo));
    }

    @ApiOperation("update信息")
    @WebProcess
    @PutMapping(value = "/courseType/update")
    public ResultCode update(@RequestBody CourseTypePo courseTypePo) {
        courseTypePo.setLId(courseTypePo.getLogin().getLId());
        return ResultCode.success(courseTypeService.save(courseTypePo));
    }

    @ApiOperation("根据id删除课程类型信息")
    @WebProcess
    @DeleteMapping(value = "/courseType/deleteById")
    public ResultCode deleteById(@RequestParam("id") Integer id) {
        return ResultCode.success(courseTypeService.deleteById(id));
    }

    @ApiOperation("批量根据id删除课程类型信息")
    @WebProcess
    @DeleteMapping(value = "/courseType/deleteByIds")
    public ResultCode deleteById(@RequestParam("ids") List<Integer> ids) {
        return ResultCode.success(courseTypeService.deleteByIds(ids));
    }


}